<template>
  <div class="content-container page-group-portrait">
    <el-row style="padding-top: 30px">
      <el-col
        :span="1"
        style="line-height: 40px; font-size: 20px; padding-left: 5px"
      >
        <router-link :to="'/group/portrait'" class="linkId">
          <i class="el-icon-arrow-left"></i>
        </router-link>
      </el-col>
      <el-col :span="4">
        <el-input v-model="form.displayName"></el-input>
      </el-col>
    </el-row>
    <div class="content-form">
      <el-collapse v-model="activeNames">
        <el-collapse-item name="1">
          <template slot="title">
            <span style="font-size: 18px; margin-left: 10px"
              ><i class="header-icon el-icon-caret-bottom"></i
              >选择画像人群的筛选条件</span
            >
          </template>
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="120px"
            style="padding: 10px"
          >
            <div>
              <div class="user-filter">
                <div class="contentLine" :style="{ height: globalHeight }">
                  <span @click="globalChangeExpressionFun">{{
                    form.globalStr
                  }}</span>
                </div>

                <div class="itemContent" id="rowContentProperties">
                  <div class="itemContent-header">
                    <i class="el-icon-plus"></i>用户属性满足
                    <el-link
                      type="primary"
                      :underline="false"
                      style="
                        padding-left: 20px;
                        float: right;
                        display: inline-block;
                      "
                      @click="addUserAttribute"
                    >
                      <i class="el-icon-plus"></i>
                      <span class="innerContent">筛选</span>
                    </el-link>
                  </div>
                  <div>
                    <div
                      class="contentLine"
                      :style="{ height: height }"
                      v-show="form.userAttributes.length > 1"
                    >
                      <span @click="changeExpressionFun">{{
                        form.attributeStr
                      }}</span>
                    </div>
                    <div class="contentProperties" id="divContentProperties">
                      <el-row v-show="form.userAttributes.length > 0">
                        <el-form-item
                          label=""
                          style="display: block; padding-left: 10px"
                          label-width="0px"
                        >
                          <div
                            v-for="(v, index) in form.userAttributes"
                            :key="index"
                            class="main-item"
                          >
                            <el-select
                              v-model="form.userAttributes[index].attributeName"
                              style="width: 120px"
                              size="small"
                              class="line-item"
                              @change="
                                userAttributeChangeFun(
                                  form.userAttributes,
                                  index
                                )
                              "
                            >
                              <el-option
                                v-for="dict in userAttributeOptions"
                                :key="dict.attributeName"
                                :label="dict.attributeRemark"
                                :value="dict.attributeName"
                              />
                            </el-select>
                            <el-select
                              v-model="
                                form.userAttributes[index].valueExpression
                              "
                              style="width: 120px"
                              size="small"
                              class="line-item"
                              @change="
                                eventPropertyChange(
                                  form.userAttributes[index],
                                  index
                                )
                              "
                            >
                              <el-option
                                v-for="dict in userPropertysOptions"
                                :key="dict.eventItemCode"
                                :label="dict.eventItemExpression"
                                :value="dict.eventItemCode"
                              />
                            </el-select>
                            <el-input
                              v-model="form.userAttributes[index].value1Str"
                              type="text"
                              controls-position="right"
                              style="width: 100px"
                              size="small"
                              class="line-item"
                              v-show="
                                form.userAttributes[index].valueExpression !=
                                'between'
                              "
                            >
                            </el-input>
                            <span
                              v-show="
                                form.userAttributes[index].valueExpression ==
                                'between'
                              "
                            >
                              <el-input
                                v-model="form.userAttributes[index].value1Str"
                                size="small"
                                type="text"
                                controls-position="right"
                                style="width: 90px"
                              >
                              </el-input
                              >与
                              <el-input
                                v-model="form.userAttributes[index].value2Str"
                                type="text"
                                controls-position="right"
                                style="width: 90px"
                              >
                              </el-input
                              >之间
                            </span>
                            <el-link
                              type="primary"
                              :underline="false"
                              style="padding-left: 20px; font-size: 18px"
                              @click="removeRule(form.userAttributes, index)"
                            >
                              <i class="el-icon-remove"></i>
                            </el-link>
                          </div>
                        </el-form-item>
                      </el-row>
                    </div>
                  </div>
                </div>

                <div class="itemContent">
                  <div class="itemContent-header">
                    <i class="el-icon-plus"></i>用户行为满足
                    <el-link
                      type="primary"
                      :underline="false"
                      style="
                        padding-left: 20px;
                        float: right;
                        display: inline-block;
                      "
                      @click="addUserActions"
                    >
                      <i class="el-icon-plus"></i>
                      <span class="innerContent">筛选</span>
                    </el-link>
                  </div>
                  <div>
                    <div
                      class="contentLine"
                      :style="{ height: actionHeight }"
                      v-show="form.userActions.length > 1"
                    >
                      <span @click="changeActionExpressionFun">{{
                        form.actionStr
                      }}</span>
                    </div>
                    <div class="contentProperties" id="divContentActions">
                      <el-row v-show="form.userActions.length > 0">
                        <el-form-item
                          label=""
                          style="display: block; padding-left: 10px"
                          label-width="0px"
                        >
                          <div
                            v-for="(v, index) in form.userActions"
                            :key="index"
                            class="main-item"
                          >
                            <span class="indexButton">{{
                              indexOptions[index]
                            }}</span>
                            <el-date-picker
                              v-model="form.userActions[index].dataRange"
                              size="small"
                              style="width: 240px"
                              value-format="yyyy-MM-dd"
                              type="daterange"
                              range-separator="-"
                              class="line-item"
                              :picker-options="pickerOptions"
                              start-placeholder="开始日期"
                              end-placeholder="结束日期"
                            ></el-date-picker>
                            <el-select
                              v-model="form.userActions[index].doThings"
                              style="width: 120px"
                              class="line-item"
                              size="small"
                              @change="
                                eventPropertyChange(
                                  form.userActions[index],
                                  index
                                )
                              "
                            >
                              <el-option label="做过" value="do" />
                              <el-option label="未做过" value="undo" />
                            </el-select>
                            <el-select
                              v-model="form.userActions[index].eventType"
                              style="width: 120px"
                              class="line-item"
                              size="small"
                              @change="eventTypeChange(funnelRuleType, index)"
                              value-key="id"
                            >
                              <el-option
                                v-for="dict in eventTypeOptions"
                                :key="dict.id"
                                :label="dict.eventTypeName"
                                :value="dict"
                              />
                            </el-select>
                            <el-link
                              type="primary"
                              :underline="false"
                              style="padding-left: 20px"
                              @click="
                                addActionEventItem(
                                  form.userActions[index].eventItems,
                                  index
                                )
                              "
                            >
                              <i class="el-icon-plus"></i>添加筛选
                            </el-link>
                            <el-link
                              type="primary"
                              :underline="false"
                              style="padding-left: 20px; font-size: 18px"
                              @click="removeActionRule(form.userActions, index)"
                            >
                              <i class="el-icon-remove"></i>
                            </el-link>
                            <el-row
                              size="small"
                              class="attach-item"
                              :key="itemIndex"
                              v-for="(item, itemIndex) in form.userActions[
                                index
                              ].eventItems"
                            >
                              <el-select
                                v-model="item.eventItem"
                                style="width: 120px"
                                class="line-item"
                                size="small"
                                value-key="id"
                              >
                                <el-option
                                  v-for="dict in allEventItemListOpts[
                                    form.userActions[index].eventType.id
                                  ]"
                                  :key="dict.id"
                                  :label="dict.eventItemName"
                                  :value="dict"
                                />
                              </el-select>
                              <el-select
                                v-model="item.eventItemExpression"
                                style="width: 120px"
                                size="small"
                                class="line-item"
                                @change="
                                  eventPropertyChange(
                                    funnelRuleType[index],
                                    index
                                  )
                                "
                              >
                                <el-option
                                  v-for="dict in eventPropertysOptions[
                                    item.eventItem.valueType
                                  ]"
                                  :key="dict.eventItemCode"
                                  :label="dict.eventItemExpression"
                                  :value="dict.eventItemCode"
                                />
                              </el-select>
                              <el-input
                                v-model="item.value1Str"
                                type="text"
                                size="small"
                                controls-position="right"
                                style="width: 100px"
                                v-show="ruleTypes3Flag == 1"
                              ></el-input>
                              <span v-show="ruleTypes3Flag == 2">
                                <el-input
                                  v-model="item.value1Str"
                                  type="text"
                                  controls-position="right"
                                  style="width: 90px"
                                ></el-input>
                                与
                                <el-input
                                  v-model="item.value2Str"
                                  type="text"
                                  controls-position="right"
                                  style="width: 90px"
                                ></el-input>
                                之间
                              </span>

                              <el-link
                                @click="
                                  removeOrderEventRule(
                                    form.userActions[index].eventItems,
                                    itemIndex
                                  )
                                "
                                type="primary"
                                style="padding-left: 20px; font-size: 18px"
                                icon="el-icon-remove"
                              ></el-link>
                            </el-row>
                            <el-row class="attach-item">
                              <el-cascader
                                v-model="form.userActions[index].eventMethod"
                                :options="
                                  allEventItemMethodOpts[
                                    form.userActions[index].eventType.id
                                  ]
                                "
                                class="line-item"
                                size="small"
                                :props="{
                                  expandTrigger: 'hover',
                                  multiple: false,
                                }"
                                @change="selectItemMethod(formItem, indexA)"
                              ></el-cascader>
                              <el-select
                                v-model="
                                  form.userActions[index].eventItemExpression
                                "
                                style="width: 120px"
                                class="line-item"
                                size="small"
                                @change="
                                  eventPropertyChange(
                                    funnelRuleType[index],
                                    index
                                  )
                                "
                              >
                                <el-option
                                  v-for="dict in eventPropertysOptions['02']"
                                  :key="dict.eventItemCode"
                                  :label="dict.eventItemExpression"
                                  :value="dict.eventItemCode"
                                />
                              </el-select>
                              <el-input
                                v-model="form.userActions[index].value1Str"
                                type="text"
                                controls-position="right"
                                style="width: 100px"
                                v-show="ruleTypes3Flag == 1"
                              ></el-input>
                              <span v-show="ruleTypes3Flag == 2">
                                <el-input
                                  v-model="form.userActions[index].value1Str"
                                  type="text"
                                  controls-position="right"
                                  style="width: 90px"
                                ></el-input
                                >与
                                <el-input
                                  v-model="form.userActions[index].value2Str"
                                  type="text"
                                  controls-position="right"
                                  style="width: 90px"
                                ></el-input
                                >之间
                              </span>
                            </el-row>
                          </div>
                        </el-form-item>
                      </el-row>
                    </div>
                  </div>
                </div>

                <div class="itemContent">
                  <div class="itemContent-header">
                    <i class="el-icon-plus"></i>行为序列
                    <el-link
                      type="primary"
                      :underline="false"
                      style="
                        padding-left: 20px;
                        float: right;
                        display: inline-block;
                      "
                      @click="addActionOrders"
                    >
                      <i class="el-icon-plus"></i>
                      <span class="innerContent">筛选</span>
                    </el-link>
                  </div>
                  <div>
                    <div
                      class="contentLine"
                      :style="{ height: orderHeight }"
                      v-show="form.userActionOrders.length > 1"
                    >
                      <span @click="changeOrderExpressionFun">{{
                        form.orderStr
                      }}</span>
                    </div>
                    <div class="contentProperties" id="divActionOrders">
                      <el-row v-show="form.userActionOrders.length > 0">
                        <el-form-item
                          label=""
                          style="display: block; padding-left: 10px"
                          label-width="0px"
                        >
                          <div
                            v-for="(v, index) in form.userActionOrders"
                            :key="index"
                            class="main-item"
                          >
                            <span class="indexButton">{{
                              indexOptions[index]
                            }}</span>
                            <el-date-picker
                              v-model="form.userActionOrders[index].dataRange"
                              size="small"
                              style="width: 240px"
                              value-format="yyyy-MM-dd"
                              type="daterange"
                              range-separator="-"
                              class="line-item"
                              :picker-options="pickerOptions"
                              start-placeholder="开始日期"
                              end-placeholder="结束日期"
                            ></el-date-picker>
                            依次做过
                            <el-link
                              type="primary"
                              :underline="false"
                              style="padding-left: 20px"
                              @click="
                                addActionOrderEventItem(
                                  form.userActionOrders[index].eventItems,
                                  index
                                )
                              "
                            >
                              <i class="el-icon-plus"></i>添加筛选
                            </el-link>
                            <el-link
                              type="primary"
                              :underline="false"
                              style="padding-left: 20px; font-size: 18px"
                              @click="
                                removeActionOrderRule(
                                  form.userActionOrders,
                                  index
                                )
                              "
                            >
                              <i class="el-icon-remove"></i>
                            </el-link>
                            <el-row
                              class="attach-item"
                              :key="itemIndex"
                              v-for="(item, itemIndex) in form.userActionOrders[
                                index
                              ].eventItems"
                            >
                              <span class="secondButton">{{
                                indexOptions[index] + "" + (itemIndex + 1)
                              }}</span>
                              <el-select
                                v-model="item.eventId"
                                style="width: 120px"
                                size="small"
                                class="line-item"
                                @change="eventTypeChange(funnelRuleType, index)"
                              >
                                <el-option
                                  v-for="dict in eventTypeOptions"
                                  :key="dict.id"
                                  :label="dict.eventTypeName"
                                  :value="dict.id"
                                />
                              </el-select>
                              <el-select
                                v-model="item.eventItem"
                                style="width: 120px"
                                size="small"
                                class="line-item"
                                value-key="id"
                              >
                                <el-option
                                  v-for="dict in allEventItemListOpts[
                                    item.eventId
                                  ]"
                                  :key="dict.id"
                                  :label="dict.eventItemName"
                                  :value="dict"
                                />
                              </el-select>
                              <el-select
                                v-model="item.eventItemExpression"
                                style="width: 120px"
                                size="small"
                                class="line-item"
                                @change="
                                  eventPropertyChange(
                                    funnelRuleType[index],
                                    index
                                  )
                                "
                              >
                                <el-option
                                  v-for="dict in eventPropertysOptions[
                                    item.eventItem.valueType
                                  ]"
                                  :key="dict.eventItemCode"
                                  :label="dict.eventItemExpression"
                                  :value="dict.eventItemCode"
                                />
                              </el-select>
                              <el-input
                                v-model="item.value1Str"
                                type="text"
                                controls-position="right"
                                style="width: 100px"
                                v-show="ruleTypes3Flag == 1"
                              ></el-input>
                              <span v-show="ruleTypes3Flag == 2">
                                <el-input
                                  v-model="item.value1Str"
                                  type="text"
                                  controls-position="right"
                                  style="width: 90px"
                                ></el-input>
                                与
                                <el-input
                                  v-model="item.value2Str"
                                  type="text"
                                  controls-position="right"
                                  style="width: 90px"
                                ></el-input>
                                之间
                              </span>

                              <el-link
                                @click="
                                  removeOrderEventRule(
                                    form.userActionOrders[index].eventItems,
                                    itemIndex
                                  )
                                "
                                type="primary"
                                style="padding-left: 20px; font-size: 18px"
                                icon="el-icon-remove"
                              ></el-link>
                            </el-row>
                          </div>
                        </el-form-item>
                      </el-row>
                    </div>
                  </div>
                </div>
              </div>
              <el-divider></el-divider>
              <div style="margin-top: 20px; padding-left: 10px">
                <el-button type="primary" @click="submitForm">保存</el-button>
              </div>
            </div>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </div>

    <!-- 画像信息 -->
    <div class="flex-btw">
      <h3>画像信息</h3>
<!--      <div class="more">-->
<!--        <el-checkbox v-model="isTGI">-->
<!--          查看TGI-->
<!--          <el-tooltip placement="top-end" effect="dark">-->
<!--            <div slot="content">-->
<!--              TGI：Target Group Index（目标群体指数）<br />-->
<!--              TGI=[「目标用户群」中某一特征的总用户数在「目标用户群」<br />-->
<!--              总用户数的占比/「全量用户」中具有该特征的总用户数在「全<br />-->
<!--              量用户」总用户数的占比]*标准数100<br />-->
<!--              TGI指数大于100，则说明该特征用户倾向较强（和平均相比）<br />-->
<!--              小于100，则说明该类用户相关倾向较弱（和平均相比）<br />-->
<!--              而等于100则表示在平均水平-->
<!--            </div>-->
<!--            <i class="el-icon-question"></i>-->
<!--          </el-tooltip>-->
<!--        </el-checkbox>-->
<!--      </div>-->
    </div>

    <el-card shadow="never" :body-style="{ padding: '20px' }">
      <el-row :gutter="20">
        <el-col :span="11" :offset="0">
          <div class="card-title flex-btw">
            <div>目标用户群</div>
            <div class="desc">计算于：{{chartData.curTime}}</div>
          </div>
          <div class="card-content">
            <el-progress
              type="circle"
              :width="80"
              :percentage="chartData.mainScale"
            ></el-progress>
            <div class="right">
              <div class="number">{{chartData.mainNum?chartData.mainNum:'0'}} <span>人</span></div>
              <div class="desc">在 全部用户 中占比 {{chartData.mainScale?chartData.mainScale:''}} %</div>
            </div>
          </div>
        </el-col>
        <el-col :span="2" :offset="0">
          <div class="vs">vs</div>
        </el-col>

        <el-col :span="11" :offset="0">
          <div class="card-title">
            对照组
            <div :style="{ margin: '0 10px' }">
              <el-select v-model="form.controlGroupId" placeholder="请选择" @change="controlGroupFun">
                <el-option-group
                  v-for="group in tagPortraitOptions"
                  :key="group.label"
                  :label="group.label">
                  <el-option
                    v-for="item in group.options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-option-group>
              </el-select>
            </div>

<!--            <el-popover placement="bottom" title="选择版本" trigger="click">-->
<!--              <div>-->
<!--                <el-radio v-model="radio1" label="1"> 最新版本 </el-radio>-->
<!--              </div>-->
<!--              <div>-->
<!--                <el-radio v-model="radio1" label="2">-->
<!--                  指定版本-->
<!--                  <el-select size="mini" v-model="value" placeholder="请选择">-->
<!--                    <el-option-->
<!--                      v-for="item in options"-->
<!--                      :key="item.value"-->
<!--                      :label="item.label"-->
<!--                      :value="item.value"-->
<!--                    >-->
<!--                    </el-option>-->
<!--                  </el-select>-->
<!--                </el-radio>-->
<!--              </div>-->
<!--              <div style="margin-top: 10px">-->
<!--                <el-button type="primary" size="small">确定</el-button>-->
<!--                <el-button size="small">取消</el-button>-->
<!--              </div>-->
<!--              <div class="popover" slot="reference">最新结果</div>-->
<!--            </el-popover>-->
          </div>
          <div class="card-content">
            <el-progress
              type="circle"
              :width="80"
              :percentage="chartData.controlScale"
            ></el-progress>
            <div class="right">
              <div class="number">{{chartData.controlNum?chartData.controlNum:''}} <span>人</span></div>
              <div class="desc">在 全部用户 中占比 {{chartData.controlScale}} %</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <div class="flex-btw" style="padding: 20px 0">
      <el-button
        @click="templateVisible = true"
        type="primary"
        icon="el-icon-menu"
        >加载模板查看</el-button
      >
<!--      <el-button-->
<!--        @click="dialogVisible = true"-->
<!--        icon="el-icon-circle-plus-outline"-->
<!--        >添加画像信息</el-button-->
<!--      >-->
    </div>
    <el-card shadow="never" :body-style="{ padding: '20px', align: 'center' }">
      <div class="explainContent">
        <div class="item">探索用户标签的分布情况</div>
        <div class="item">观察用户的历史累计行为</div>
        <div class="item">了解用户的特征分布结构</div>
      </div>
<!--      <el-button-->
<!--        class="explainBtn"-->
<!--        @click="dialogVisible = true"-->
<!--        round-->
<!--        type="primary"-->
<!--        >添加第一个画像信息</el-button-->
<!--      >-->
    </el-card>

    <div style="height:20px"></div>
<!--   两者都有 -->
    <el-row :gutter="20">
      <el-col :span="controlChartList.length>0?12:24">
        <el-row :gutter="20">
            <el-col  v-for="i in chartList" :key='i.id' :span="controlChartList.length?24:12" style="margin-top:10px">
              <el-card shadow="hover" v-loading="chartLoading" :body-style="{ padding: '10px' }">
                <div slot="header">
                  <div class="chart-header">
                    <div>
                      <div>{{i.componentName}}</div>
                      <div class="chart-header-desc">{{i.remark}}</div>
                    </div>
                    <div>
                      <!--                <el-select v-model="value" clearable filterable >-->
                      <!--                  <el-option v-for="item in itemOptions"-->
                      <!--                    :key="item.value"-->
                      <!--                    :label="item.label"-->
                      <!--                    :value="item.value">-->
                      <!--                  </el-option>-->
                      <!--                </el-select>-->
                      <!--                <el-dropdown style="margin-left:10px">-->
                      <!--                  <el-button size="medium">-->
                      <!--                    <i class="el-icon-more"></i>-->
                      <!--                  </el-button>-->

                      <!--                  <el-dropdown-menu slot="dropdown">-->
                      <!--                    <el-dropdown-item>编辑</el-dropdown-item>-->
                      <!--                    <el-dropdown-item>删除</el-dropdown-item>-->
                      <!--                  </el-dropdown-menu>-->
                      <!--                </el-dropdown>-->
                    </div>
                  </div>
                </div>
                <!-- card body -->
                <BarChart v-if="i.chartType === 'bar'" :chartData="i.xdata" :yData="i.ydata" />
                <PieChart v-if="i.chartType === 'pie'" :chartData="i.xdata" />
                <RingChart v-if="i.chartType === 'ring'" :chartData="i.xdata" />
                <PillarChart v-if="i.chartType === 'pillar'" :chartData="i.xdata" />
                <CateChart v-if="i.chartType === 'cate'" :chartData="i.zdata" :yData="i.ydata" :xData="i.xdata" />
              </el-card>
            </el-col>
        </el-row>
      </el-col>
      <el-col span="12" v-if="controlChartList.length>0">
        <el-row :gutter="20">
          <el-col  v-for="i in controlChartList" :key='i.id'  :span="24" style="margin-top:10px">
            <el-card shadow="hover" v-loading="chartLoading" :body-style="{ padding: '10px'}" v-if="form.controlGroupId">
              <div slot="header">
                <div class="chart-header">
                  <div>
                    <div>{{'对照组-'+i.componentName}}</div>
                    <div class="chart-header-desc">{{i.remark}}</div>
                  </div>
                  <div>
                    <!--                <el-select v-model="value" clearable filterable >-->
                    <!--                  <el-option v-for="item in itemOptions"-->
                    <!--                    :key="item.value"-->
                    <!--                    :label="item.label"-->
                    <!--                    :value="item.value">-->
                    <!--                  </el-option>-->
                    <!--                </el-select>-->
                    <!--                <el-dropdown style="margin-left:10px">-->
                    <!--                  <el-button size="medium">-->
                    <!--                    <i class="el-icon-more"></i>-->
                    <!--                  </el-button>-->

                    <!--                  <el-dropdown-menu slot="dropdown">-->
                    <!--                    <el-dropdown-item>编辑</el-dropdown-item>-->
                    <!--                    <el-dropdown-item>删除</el-dropdown-item>-->
                    <!--                  </el-dropdown-menu>-->
                    <!--                </el-dropdown>-->
                  </div>
                </div>
              </div>
              <!-- card body -->
              <BarChart v-if="i.chartType === 'bar'" :chartData="i.xdata" :yData="i.ydata" />
              <PieChart v-if="i.chartType === 'pie'" :chartData="i.xdata" />
              <RingChart v-if="i.chartType === 'ring'" :chartData="i.xdata" />
              <PillarChart v-if="i.chartType === 'pillar'" :chartData="i.xdata" />
              <CateChart v-if="i.chartType === 'cate'" :chartData="i.zdata" :yData="i.ydata" :xData="i.xdata" />
            </el-card>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

   <!-- 加载模板弹窗 -->
    <el-dialog title="加载模板查看" :visible.sync="templateVisible" width="45%">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card>
            <div @click="templateModelFun(1)" class="template-card">
              <i class="el-icon-s-grid icon"></i>
              <div>
                <div class="tem-title">购买意向分析</div>
                <div class="tem-desc">未添加备注</div>
                <div class="tem-explain">管理员 创建于2022-01-01</div>
              </div>
            </div>

          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card class="box-card">
            <div @click="templateModelFun(2)" class="template-card">
              <i class="el-icon-s-grid icon"></i>
              <div>
                <div class="tem-title">用户与消费偏好模板</div>
                <div class="tem-desc">未添加备注</div>
                <div class="tem-explain">管理员 创建于2022-01-01</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <span slot="footer" class="dialog-footer">
        <el-button @click="templateVisible = false">取 消</el-button>
        <el-button type="primary" @click="templateVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 添加画像信息弹窗 -->
    <el-dialog title="添加画像信息" :visible.sync="dialogVisible" width="650px" :close-on-click-modal="false">
      <el-form ref="addform" :model="addform"  label-width="100px">
        <div class="dialog-title">名称</div>
        <el-input
          placeholder=""
          size="medium"
          v-model="addform.componentName"
          style="width: 200px"
          clearable
        ></el-input>
        <div class="dialog-title">类型</div>
        <el-radio v-model="addform.portraitType" label="tagProperty" border>标签属性分布</el-radio>
        <el-radio v-model="addform.portraitType" label="userAction" border>用户行为指标</el-radio>
        <el-radio v-model="addform.portraitType" label="userPorperty" border>用户特征分布</el-radio>

        <div v-if="addform.addChartType === 'tagProperty'">
          <div class="dialog-title">标签</div>
          <el-select size="mini" v-model="value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div v-if="addform.addChartType === 'userAction'">
          <div class="dialog-title">指标</div>
          用户群在
          <el-date-picker
            v-model="datePick"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
          完成
          <el-select size="mini" v-model="addActionValue" placeholder="请选择">
            <el-option
              v-for="item in addAction"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select size="mini" v-model="addDescValue" placeholder="请选择">
            <el-option
              v-for="item in addDesc"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>



        <div class="dialog-title">图像</div>
        <el-radio v-model="addform.chartType" label="bar" border>条形图</el-radio>
        <el-radio v-model="addform.chartType" label="ring" border>环形图</el-radio>
      </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addPortraitChart">确 定</el-button>
        </span>

    </el-dialog>
  </div>
</template>

<script>
import { getUserAttributeList } from "@/api/config/userattribute";
import {
  listAllPropertyResult,
  listPropertyByType,
} from "@/api/config/property";
import { allEventItemsOptions, eventTypeOptions } from "@/api/config/item";
import { getAllEventMethodResult } from "@/api/config/emethod";
import { addGroup, addUserGroup, updateGroup } from "@/api/base/group";
import {addUserPortrait, getAllTagPortraitList, getUserPortraitById, queryUserPortrait} from "@/api/tags/portrait";

import RingChart from './components/ringChart.vue'
import PieChart from './components/pieChart.vue'
import BarChart from './components/barChart.vue'
import PillarChart from './components/pillarChart.vue'
import CateChart from './components/cateChart.vue'
import {getPortraitTemplate} from "@/api/tags/template";

export default {
  name: "portraitCreate",
  components: {
    RingChart,
    BarChart,
    PieChart,
    PillarChart,
    CateChart
  },
  data() {
    return {
      // 遮罩层
      percentage:0,
      loading: true,
    rules: {},
      activeNames: "1",
      // 日期选择
      chartLoading:false,
      addActionValue: '',
      addAction: [
        {
          value: '7',
          label: '启动app'
        },
        {
          value: '10',
          label: '前10项'
        },
      ],
      addDescValue: '',
      addDesc: [
        {
          value: '7',
          label: '总次数'
        },
        {
          value: '10',
          label: '启动时长'
        },
      ],
      datePick: '',
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      addform:{
        componentName:"",
        portraitType:"tagProperty",
      },
      // 表单参数
      form: {
        displayName: "未命名用户群画像1",
        userAttributes: [],
        userActions: [],
        userActionOrders: [],
        limitNumberFlag: false,
        limitType: "1",
        resultNum: 0,
        buildType: "01",
        globalStr: "且",
        attributeStr: "且",
        actionStr: "且",
        orderStr: "且",
        controlGroupId:null,
        portraitTemplateItems:[]
      },
      active: 0,
      ruleTypes3Flag: 0,
      userPortraitOption: {},
      userPropertysOptions: [],
      userAttributeOptions: [],
      eventItemMethosOpts: [],
      allEventItemListOpts: {},
      eventPropertysOptions: {},
      allEventItemMethodOpts: {},
      childHeight: 0,
      actionChildHeight: 0,
      globalHeight: "186px",
      height: "0px",
      actionHeight: "0px",
      orderHeight: "0px",
      eventTypeOptions: [],
      tagPortraitOptions: [],
      indexOptions: [
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z",
      ],
      calcGroupNum: "",
      itemOptions: [
        {
          value: '7',
          label: '前7项'
        },
        {
          value: '10',
          label: '前10项'
        },
        {
          value: '20',
          label: '前20项'
        },
      ],
	    chartData: {
		    mainNum: 0,
        mainScale: 0,
		    controlNum: 0,
        controlScale: 0,
        curTime: ""
	    },
      chartList: [],
      controlChartList: [],

      // 画像信息
      isTGI: false,
      options: [],
      value: "",
      radio1: "",
      addChartType: "",
      radio3: "",
      dialogVisible: false,
      templateVisible: false,
    };
  },
  created() {
    this.getAllTagPortraitList();
    this.getEventTypeList();
    this.getAllEventItemsList();
    this.getUserAttributeList();
    let portraitId=this.$route.query.id;
    if(portraitId){
      getUserPortraitById(portraitId).then((response) => {
        this.currentProtrait = response.data;
        let portraitObj = JSON.parse(this.currentProtrait.portraitRule);
        this.form=portraitObj;
        this.chartList=this.form.portraitTemplateItems;
        this.queryPortraitChartData();
      });
    }

  },
  methods: {
    getEventTypeList() {
      var param = {
        status: "Y",
      };
      eventTypeOptions(param).then((response) => {
        this.eventTypeOptions = response.data;
      });
    },

    getAllTagPortraitList() {
      getAllTagPortraitList().then((response) => {
        this.tagPortraitOptions = response.data;
      });
    },
    getAllEventItemsList() {
      allEventItemsOptions().then((response) => {
        this.allEventItemListOpts = response.data;
      });
      listAllPropertyResult().then((response) => {
        this.eventPropertysOptions = response.data;
      });
      getAllEventMethodResult().then((response) => {
        this.allEventItemMethodOpts = response.data;
      });
    },

    getUserAttributeList() {
      var param = {
        status: "Y",
      };
      getUserAttributeList(param).then((response) => {
        this.userAttributeOptions = response.data;
      });
    },

    getPortraitTemplate(id) {
      getPortraitTemplate(id).then((response) => {
        console.log(response.data)
        this.userPortraitOption = response.data;
        this.chartList=response.data.basePortraitTemplateItemsList;
        this.queryPortraitChartData();
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      let requestJson = JSON.stringify(this.form);
      addUserPortrait({
        requestJson: requestJson,
      }).then((response) => {
        if (response.code === 200) {
          this.msgSuccess("新增成功");
          this.open = false;
        } else {
          this.msgError(response.msg);
        }
      });
    },

    userAttributeChangeFun(array, index) {
      array[index].valueExpression = "";
      array[index].value1Str = "";
      array[index].value2Str = "";
      listPropertyByType({
        valueType: array[index].dataType,
      }).then((response) => {
        this.userPropertysOptions = response.data;
      });
    },

    eventPropertyChange(obj, index) {
      if (obj.eventItemExpression == "between") {
        this.ruleTypes3Flag = 2;
      } else {
        this.ruleTypes3Flag = 1;
      }
    },

    addUserAttribute() {
      let userAttribute = {
        attributeName: "",
        attributeRemark: "",
        valueExpression: "",
        dataType: "",
        valueType: "",
        value1Str: "",
        value2Str: "",
      };
      this.form.userAttributes.push(userAttribute);
      this.getGlobalHeight();
    },

    addUserActions() {
      let userAction = {
        dataRange: [],
        eventItems: [],
        doThings: "",
        eventType: {},
      };
      this.form.userActions.push(userAction);
      this.getGlobalHeight();
    },
    addActionOrders() {
      let actionOrder = {
        dataRange: [],
        eventItems: [],
      };
      this.form.userActionOrders.push(actionOrder);
      this.getGlobalHeight();
    },

    getGlobalHeight() {
      this.$nextTick(() => {
        let height =
          parseInt(this.getDocumentHeight("divContentProperties")) - 10;
        let actionHeight =
          parseInt(this.getDocumentHeight("divContentActions")) - 10;
        let orderHeight =
          parseInt(this.getDocumentHeight("divActionOrders")) - 10;
        let globalHeight = height + actionHeight + orderHeight + 186 + 30;
        this.height = height + "px";
        this.actionHeight = actionHeight + "px";
        this.orderHeight = orderHeight + "px";
        this.globalHeight = globalHeight + "px";
      });
    },

    getDocumentHeight(id) {
      let div = document.getElementById(id);
      let height = window.getComputedStyle(div).getPropertyValue("height");
      return height;
    },

    removeRule(array, index) {
      array.splice(index, 1);
      this.getGlobalHeight();
    },
    removeActionRule(array, index) {
      array.splice(index, 1);
      this.getGlobalHeight();
    },
    removeActionOrderRule(array, index) {
      array.splice(index, 1);
      this.getGlobalHeight();
    },

    setDivHeight() {
      let height = this.getDocumentHeight("divContentProperties");
    },

    handleClose() {
      this.drawer = false;
    },

    buildUserGroup() {
      this.open = false;
      this.drawer = true;
      this.form.buildType = "01";
    },

    prevSteps() {
      this.active = 0;
    },
    changeSteps() {
      if (this.active < 2) {
        this.active++;
      }
      if (this.active === 2) {
        this.active = 1;
        let requestJson = JSON.stringify(this.form);
        addUserGroup({
          requestJson: requestJson,
        }).then((response) => {
          if (response.code === 200) {
            this.msgSuccess("新增成功");
            this.drawer = false;
            this.getList();
          } else {
            this.msgError(response.msg);
          }
        });
      }
    },
    globalChangeExpressionFun() {
      this.form.globalStr = this.form.globalStr === "且" ? "或" : "且";
    },
    changeExpressionFun() {
      this.form.attributeStr = this.form.attributeStr === "且" ? "或" : "且";
    },

    changeActionExpressionFun() {
      this.form.actionStr = this.form.actionStr === "且" ? "或" : "且";
    },
    changeOrderExpressionFun() {
      this.form.orderStr = this.form.orderStr === "且" ? "或" : "且";
    },

    addActionEventItem(itemArray) {
      let eventItem = {
        eventItem: {},
        eventItemExpression: "",
        valueType: "",
        value1Str: "",
        value2Str: "",
      };
      itemArray.push(eventItem);
      this.getGlobalHeight();
    },

    addActionOrderEventItem(itemArray) {
      let eventItem = {
        eventId: "",
        eventItem: {},
        eventItemExpression: "",
        valueType: "",
        value1Str: "",
        value2Str: "",
      };
      itemArray.push(eventItem);
      this.getGlobalHeight();
    },

    removeOrderEventRule(itemArray, index) {
      itemArray.splice(index, 1);
      this.getGlobalHeight();
    },
    templateModelFun(id){
      this.templateVisible=false;
      this.getPortraitTemplate(id);
    },
    addPortraitChart(){
      this.dialogVisible = false;
      this.addform.xdata=[];
      this.addform.ydata=[];
      this.addform.zdata=[];
      console.log(this.addform);
      this.chartList.push(this.addform);
      this.addform={};
      this.queryPortraitChartData();
    },

    queryPortraitChartData(){
      this.form.portraitTemplateItems=this.chartList;
      this.chartLoading=true;
      let requestJson = JSON.stringify(this.form);
      queryUserPortrait({
        requestJson: requestJson,
      }).then((response) => {
        this.chartLoading=false;
        console.log(response);
        this.chartList=response.data.chartList;
        this.chartData=response.data.chartData;
        this.controlChartList=response.data.controlChartList;
        console.log("chartData:"+this.chartData);
      }).catch(v=>{
        this.chartLoading=false;
      });;
    },
    controlGroupFun(){
      this.queryPortraitChartData();
    }
  },
};
</script>
<style rel="stylesheet/scss" lang="scss">

.chart-header {
  display: flex;
  justify-content: space-between;
  &-desc {
    font-size: 14px;
    color: #ccc;
  }
}

.template-card {
  display: flex;
  .icon {
    font-size: 36px;
  }


  .tem-{
    &title {
      font-size: 20px;
    }
    &desc {
      font-size: 14px;
      color: #aaa;
    }
    &explain {
      font-size: 14px;
      color: #ccc;
    }
  }
}

.content-container {
  width: 100%;
  padding: 0 20px 20px;
  background: #f7f9fa;
  display: flex;
  flex-direction: column;
}

.content-title {
  font-size: 15px;
  font-weight: bold;
}

.content-form {
  width: 100%;
  background-color: #fff;
  border-radius: 3px;
  margin-top: 30px;
  padding-top: 20px;
}

.ant-modal-body {
  padding: 24px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;

  .index__container___2sti8 {
    background: #fff;
    width: 540px;
    padding: 15px 20px;
    border-radius: 3px;
    border: 1px solid #e9f0f7;
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;

    .index__blockLogo___hnHAh {
      width: 73px;
      text-align: center;
    }

    .index__blockDivideLine___2nKYu:before {
      content: "";
      border-left: 1px solid #e0e6ed;
      height: 40px;
      display: block;
      margin: 0 27px;
    }

    .index__blockDesc___EXACq {
      display: inline-block;
      width: 383px;
    }
  }

  .index__container___2sti8:hover {
    border-color: #00bf8a;
  }
}

.page-group-portrait {
  .user-filter {
    position: relative;
    & > .contentLine {
      top: 0;
    }
    // background-color: red;
  }
  .itemContent {
    border: 1px solid #e0e6ed;
    padding: 10px 10px;
    height: auto;
    margin-left: 40px;
    border-radius: 3px;
    margin-bottom: 15px;
    position: relative;
    .itemContent-header {
      height: 30px;
      line-height: 30px;
    }
  }

  .contentLine {
    width: 2px;
    position: absolute;
    background-color: #d6efe8;
    top: 42px;
    left: 22px;
  }

  .contentLine > span {
    position: relative;
    display: block;
    top: 50%;
    left: -12px;
    right: 0;
    width: auto;
    min-width: 25px;
    height: 25px;
    transform: translateY(-50%);
    border-radius: 3px;
    background: #d6efe8;
    color: #00d166;
    font-size: 14px;
    font-weight: 700;
    line-height: 25px;
    text-align: center;
    cursor: pointer;
    user-select: none;
  }

  .contentProperties {
    width: 99%;

    .el-form-item {
      margin-bottom: 0px;
    }
  }

  .el-drawer {
    overflow: scroll;
  }

  .linkId {
    color: #559ff0;
    text-decoration: none;
    background-color: transparent;
    outline: none;
    cursor: pointer;
    transition: color 0.3s;
  }

  .caclResult {
    margin-left: 10px;
    color: #475669;
    font-size: 14px;
    line-height: 25px;
    font-weight: bold;
  }

  .indexButton {
    display: inline-block;
    background: #2dca93;
    color: #fff;
    width: 20px;
    height: 20px;
    border-radius: 2px;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    position: relative;
    margin-right: 14px;
  }

  .secondButton {
    width: 22px;
    height: 22px;
    line-height: 22px;
    background: #f5faff;
    border-radius: 3px;
    border: 1px solid #d3dce6;
    font-size: 12px;
    color: #99a9bf;
    margin-right: 8px;
  }

  .contentProperties {
    margin-left: 30px;
  }

  .line-item {
    margin-right: 6px;
  }
  .main-item {
    margin-bottom: 8px;
  }
  .attach-item {
    margin: 0 0 6px 34px;
  }
}

//画像信息
.flex-btw {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.vs {
  display: inline-block;
  color: white;
  background: #f5b126;
  padding: 5px 10px;
  border-radius: 5px;
  margin-top: 50%;
  margin-left: 25%;
}

.card-title {
  display: flex;
  align-items: center;
  font-size: 18px;
  margin-bottom: 10px;
  .desc {
    color: #ccc;
    font-size: 14px;
  }
  .popover {
    color: #559ff0;
    font-size: 14px;
  }
}

.card-content {
  display: flex;
  align-items: center;
  .right {
    margin-left: 20px;
  }
  .number {
    font-size: 24px;
    span {
      font-size: 14px;
      color: #ccc;
    }
  }
  .desc {
    color: #ccc;
    font-size: 14px;
    margin-top: 5px;
  }
}

.explainContent {
  display: flex;
  justify-content: space-between;
  .item {
    width: 32%;
    border-radius: 10rpx;
    border: 1px solid #ccc;
    text-align: center;
    padding: 30px;
  }
}
.explainBtn {
  margin-left: 50%;
  transform: translateX(-50%);
  margin-top: 20px;
}

.dialog-title {
  margin: 10px 0;
}
</style>
